package blogData.action;

import account.entity.AccountVO;
import blogData.entity.BlogDataVO;
import blogData.service.BlogDataService;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import util.FileInputOut;
import util.ObtainOther;

import java.io.File;

public class BlogDataAction extends ActionSupport {

    private final static Log LOGER = LogFactory.getLog(BlogDataAction.class);

    private final static String uploadDir = ObtainOther.obtainMessage("blogImg");



    private BlogDataService blogDataService;

    private BlogDataVO blogDataVO;

    private AccountVO accountVO;

    private Integer BlogDataId;

    private String message;

    private File imgFile;

    private String imgFileType;

    public String getImgFileType() {
        return imgFileType;
    }

    public void setImgFileType(String imgFileType) {
        this.imgFileType = imgFileType;
    }

    public File getImgFile() {
        return imgFile;
    }

    public void setImgFile(File imgFile) {
        this.imgFile = imgFile;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public BlogDataVO getBlogDataVO() {
        return blogDataVO;
    }

    public void setBlogDataVO(BlogDataVO blogDataVO) {
        this.blogDataVO = blogDataVO;
    }

    public AccountVO getAccountVO() {
        return accountVO;
    }

    public void setAccountVO(AccountVO accountVO) {
        this.accountVO = accountVO;
    }

    public Integer getBlogDataId() {
        return BlogDataId;
    }

    public void setBlogDataId(Integer id) {
        this.BlogDataId = BlogDataId;
    }

    public BlogDataService getBlogDataService() {
        return blogDataService;
    }

    public void setBlogDataService(BlogDataService blogDataService) {
        this.blogDataService = blogDataService;
    }

    /**
     * 添加个人资料
     *
     * @return
     */
    public String addBlogData() {
        try {
            accountVO = ObtainOther.obtainAccount();
            if (!("").equals(imgFileType) && blogDataVO.getBlogImage() != null) {
                blogDataVO.setBlogImage(FileInputOut.fileInputOut(blogDataVO.getBlogImage(), imgFile, uploadDir));
            }
            blogDataVO = blogDataService.addBlogDataVO(blogDataVO, accountVO);

            return SUCCESS;
        } catch (Exception e) {
            message=ObtainOther.obtainMessage("addBlogDataError");
            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

    /**
     * 更新个人资料
     *
     * @return
     */

    public String updateBlogData() {
        try {
            if (!("").equals(imgFileType) && blogDataVO.getBlogImage() != null ) {
                blogDataVO.setBlogImage(FileInputOut.fileInputOut(blogDataVO.getBlogImage(), imgFile, uploadDir));
            }
            blogDataVO = blogDataService.updateBlogDataVO(blogDataVO);
            message=ObtainOther.obtainMessage("updateBlogDataSuccess");
            return SUCCESS;
        } catch (Exception e) {
            message=ObtainOther.obtainMessage("updateBlogDataError");
            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

    public String initBlogData() {
        Integer blogDataVoId = 0;
        try {
            blogDataVoId = ObtainOther.obtainId();
            blogDataVO = blogDataService.findBlogDataVOById(blogDataVoId);
            if (blogDataVO == null) {
                return INPUT;
            } else {
                return SUCCESS;
            }
        } catch (Exception e) {
            message=ObtainOther.obtainMessage("loadBlogDataError");
            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

    public String loadBlogData() {
        Integer blogDataVoId = 0;
        try {
            blogDataVoId = ObtainOther.obtainId();
            blogDataVO = blogDataService.findBlogDataVOById(blogDataVoId);
            return SUCCESS;
        } catch (Exception e) {
            message=ObtainOther.obtainMessage("loadBlogDataError");

            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

}
